<?php
/**
 * 模型助手
 * @package lib.helpers
 */
class CModelHelper {
	/**
	 * 按ID保存$params的映射
	 * @param string $modelName
	 * @param mixed $id 可为数组或数值
	 * @param array $params 字段=>值的映射
	 * @return array $models
	 */
	static function saveAttributesByPks($modelName, $id, $params) {
		$model = new $modelName;
		$pk = $model->tableSchema->primaryKey;
		$ids = is_array($id) ? $id : array($id);
		$criteria = new CDbCriteria();
		$criteria->addInCondition($pk, $ids);
		$models = $model->findAll($criteria);
		foreach($models as $model) {
			foreach($params as $k=>$v) {
				$model->$k = $v;
			}
			$model->save();
		}
		return $models;
	}
}

?>